# coding=utf-8
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from backstage.system.utils import view_funcs


class Hotwords(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def post(self, request):
        """
        设置搜索热词
        """
        hotwords = request.data.get("words", "")
        hotwords = [hotword.strip() for hotword in hotwords.split("|")
                    if hotword.strip()]
        view_funcs.update_hotwords(hotwords)
        return Response({
            "status": 200,
            "msg": "success",
        })

    def get(self, request):
        """
        获取搜索热词
        """
        hotwords = view_funcs.get_hotwords()
        return Response({
            "status": 200,
            "msg": "success",
            "data": hotwords,
        })
